﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EWDTAssignment.App_Code;
using System.Data;
using System.IO;
using System.Text;

namespace EWDTAssignment
{
    public partial class Customize : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //REMEMBER TO DELETE
            Session["AccountID"] = 1;

            if (!IsPostBack)
            {
                if (Session["AccountID"] != null)
                {
                    hideAll();
                    Dictionary<string, double> customPrice = new Dictionary<string, double>();
                    ResetCustomPrice(customPrice);
                    Session["customPrice"] = customPrice;

                    if (Request.QueryString["Action"] != null)
                    {
                        if (Request.QueryString["Action"] == "Edit")
                        {
                            if (Session["selectEditSet"] != null)
                            {
                                populateCustomizeEditFromSet(Convert.ToInt32(Session["selectEditSet"]));
                                rblstType.Visible = false;
                                lblMsg2.Text = "Editing Set ID#" + Session["selectEditSet"];
                                lblMsg1.Visible = false;
                                lblMsg2.Visible = true;
                                lblMsg3.Visible = false;
                                btnSubmit.Visible = false;
                                btnUpdate.Visible = true;
                            }
                        }
                    }

                }
                else
                {
                    Response.Redirect("Login.aspx");
                }

                /*if (Request.UrlReferrer != null)
                {
                    if (Request.UrlReferrer.LocalPath.ToString() == "/RetrieveSets.aspx")
                    {

                    }
                }*/

                

                

                
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlMB.SelectedIndex != -1)
            {
                clearAll();
                ddlMB.Items.Clear();
                Dictionary<string, double> customPrice = (Dictionary<string, double>) Session["customPrice"];
                ResetCustomPrice(customPrice);
                chkbxDualGFX.Checked = false;
                hideAll();
            }
            int type = Convert.ToInt32(rblstType.SelectedValue);

            List<Products> pList = EntityLogic.RetrieveProducts("Motherboard", type);

            //ddlMB.DataSource = dt;
            //ddlMB.DataMember = "ProductName";
            //ddlMB.DataTextField = dt.Columns["ProductName"].ToString();
            //ddlMB.DataBind();

            ListItem li = new ListItem("[Select One]", "0");
            ddlMB.Items.Add(li);
            foreach (Products p in pList)
            {
                li = new ListItem(p.ProductName, p.ProductID.ToString());
                ddlMB.Items.Add(li);
            }
            lblMsg2.Visible = true;
            lblMB.Visible = true;
            ddlMB.Visible = true;
            Label2.Visible = true;
        }

        protected void ddlMB_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                clearAll();

                Products proMB = EntityLogic.RetrieveProductByID(Convert.ToInt32(ddlMB.SelectedValue));

                if (proMB != null)
                {
                    Dictionary<string, string> mbFlag = BusinessLogic.DelimitCSV(proMB.Flag);
                    
                    int val = Convert.ToInt32(rblstType.SelectedValue);

                    List<Products> cpuList = EntityLogic.RetrieveProducts("CPU", val, mbFlag["socket"]);
                    List<Products> memList = EntityLogic.RetrieveProducts("RAM", val, mbFlag["ram"]);
                    List<Products> gfxList = EntityLogic.RetrieveProducts("GFX", val, mbFlag["exp1"]);
                    List<Products> hddList = EntityLogic.RetrieveProducts("HDD", val, mbFlag["hdd"]);
                    List<Products> scList = EntityLogic.RetrieveProducts("Soundcard", val, mbFlag["exp2"]);
                    List<Products> psuList = EntityLogic.RetrieveProducts("PSU", val);
                    List<Products> casingList = EntityLogic.RetrieveProducts("Casing", val, mbFlag["form"]);

                    ListItem li = new ListItem("[Select One]", "0");

                    populateDDL(cpuList, ddlCPU);
                    populateDDL(memList, ddlMemory);
                    populateDDL(gfxList, ddlGFX);
                    populateHDD(Convert.ToInt32(mbFlag["hddN"]), hddList);
                    populateDDL(scList, ddlSoundcard);
                    populateDDL(psuList, ddlPSU);
                    populateDDL(casingList, ddlCasing);

                    if (mbFlag["SLI"] == "1" || mbFlag["CrossFireX"] == "1")
                    {
                        chkbxDualGFX.Visible = true;
                    }
                    getDetails(imgMB, ddlMB, lblMBSpec, lblMBPrice, "MB");

                    lblMsg3.Visible = true;
                    lblCPU.Visible = true;
                    ddlCPU.Visible = true;
                    Label3.Visible = true;
                    lblMemory.Visible = true;
                    ddlMemory.Visible = true;
                    Label4.Visible = true;
                    lblGFX.Visible = true;
                    ddlGFX.Visible = true;
                    Label5.Visible = true;
                    btnSubmit.Visible = true;
                    btnUpdate.Visible = false;
                    lblSound.Visible = true;
                    ddlSoundcard.Visible = true;
                    Label7.Visible = true;
                    lblPSU.Visible = true;
                    ddlPSU.Visible = true;
                    Label8.Visible = true;
                    lblCasing.Visible = true;
                    ddlCasing.Visible = true;
                    Label9.Visible = true;
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void clearAll()
        {
            ddlCPU.Items.Clear();
            ddlMemory.Items.Clear();
            ddlGFX.Items.Clear();
            ddlHDD1.Items.Clear();
            ddlHDD2.Items.Clear();
            ddlHDD3.Items.Clear();
            ddlHDD4.Items.Clear();
            ddlHDD5.Items.Clear();
            ddlHDD6.Items.Clear();
            ddlSoundcard.Items.Clear();
            ddlPSU.Items.Clear();
            ddlCasing.Items.Clear();
        }

        protected void hideAll()
        {
            lblMsg2.Visible = false;
            lblMsg3.Visible = false;
            lblMB.Visible = false;
            ddlMB.Visible = false;

            lblCPU.Visible = false;
            lblMemory.Visible = false;
            lblGFX.Visible = false;
            lblHDD.Visible = false;
            lblSound.Visible = false;
            lblPSU.Visible = false;
            lblCasing.Visible = false;

            chkbxDualGFX.Visible = false;

            ddlCPU.Visible = false;
            ddlMemory.Visible = false;
            ddlGFX.Visible = false;
            ddlHDD1.Visible = false;
            ddlHDD2.Visible = false;
            ddlHDD3.Visible = false;
            ddlHDD4.Visible = false;
            ddlHDD5.Visible = false;
            ddlHDD6.Visible = false;
            ddlSoundcard.Visible = false;
            ddlPSU.Visible = false;
            ddlCasing.Visible = false;

            imgMB.Visible = false;
            imgCPU.Visible = false;
            imgMemory.Visible = false;
            imgGFX.Visible = false;
            imgHDD1.Visible = false;
            imgSoundcard.Visible = false;
            imgPSU.Visible = false;
            imgCasing.Visible = false;

            lblCurrentHDD.Visible = false;
            lblCurrentHDD0.Visible = false;
            lblMBSpec.Visible = false;
            lblCPUSpec.Visible = false;
            lblMemSpec.Visible = false;
            lblGFXSpec.Visible = false;
            lblHDD1Spec.Visible = false;
            lblSoundSpec.Visible = false;
            lblPSUSpec.Visible = false;
            lblCasingSpec.Visible = false;

            lblMBPrice.Visible = false;
            lblCPUPrice.Visible = false;
            lblMemPrice.Visible = false;
            lblGFXPrice.Visible = false;
            lblHDD1Price.Visible = false;
            lblSoundPrice.Visible = false;
            lblPSUPrice.Visible = false;
            lblCasingPrice.Visible = false;
            lblHDDTPrice.Visible = false;

            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;
            Label7.Visible = false;
            Label8.Visible = false;
            Label9.Visible = false;
            Label10.Visible = false;
            Label11.Visible = false;

            lblTotal0.Visible = false;

            btnSubmit.Visible = false;
            btnUpdate.Visible = false;
        }

        protected void populateHDD(int hddNum, List<Products> hddList)
        {
            populateDDL(hddList, ddlHDD1);
            populateDDL(hddList, ddlHDD2);
            populateDDL(hddList, ddlHDD3);
            populateDDL(hddList, ddlHDD4);
            
            lblHDD.Visible = true;
            ddlHDD1.Visible = true;
            ddlHDD2.Visible = true;
            ddlHDD3.Visible = true;
            ddlHDD4.Visible = true;
            Label6.Visible = true;
            Label11.Visible = true;
            lblCurrentHDD.Visible = false;
            lblCurrentHDD0.Visible = false;

            if (hddNum > 4)
            {
                populateDDL(hddList, ddlHDD5);
                ddlHDD5.Visible = true;
            }

            if (hddNum > 5)
            {
                populateDDL(hddList, ddlHDD6);
                ddlHDD6.Visible = true;
            }

        }

        protected void populateDDL(List<Products> pList, DropDownList ddl)
        {
            ListItem li = new ListItem("[Select One]", "0");

            ddl.Items.Add(li);
            foreach (Products p in pList)
            {
                li = new ListItem(p.ProductName, p.ProductID.ToString());
                ddl.Items.Add(li);
            }
        }

        #region Selected Index Changed
        protected void ddlCPU_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgCPU, ddlCPU,lblCPUSpec,lblCPUPrice, "CPU");
        }

        protected void ddlMemory_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgMemory, ddlMemory, lblMemSpec, lblMemPrice, "RAM");
        }

        protected void ddlGFX_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgGFX, ddlGFX, lblGFXSpec,lblGFXPrice, "GFX");
        }

        protected void ddlHDD1_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgHDD1, ddlHDD1, lblHDD1Spec, lblHDD1Price, "HDD1");
            lblCurrentHDD0.Text = "1";
            lblCurrentHDD.Visible = true;
            lblCurrentHDD0.Visible = true;
        }

        protected void ddlHDD2_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgHDD1, ddlHDD2, lblHDD1Spec, lblHDD1Price, "HDD2");
            lblCurrentHDD0.Text = "2";
            lblCurrentHDD.Visible = true;
            lblCurrentHDD0.Visible = true;
        }

        protected void ddlHDD3_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgHDD1, ddlHDD3, lblHDD1Spec, lblHDD1Price, "HDD3");
            lblCurrentHDD0.Text = "3";
            lblCurrentHDD.Visible = true;
            lblCurrentHDD0.Visible = true;
        }

        protected void ddlHDD4_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgHDD1, ddlHDD4, lblHDD1Spec, lblHDD1Price, "HDD4");
            lblCurrentHDD0.Text = "4";
            lblCurrentHDD.Visible = true;
            lblCurrentHDD0.Visible = true;
        }

        protected void ddlHDD5_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgHDD1, ddlHDD5, lblHDD1Spec, lblHDD1Price, "HDD5");
            lblCurrentHDD0.Text = "5";
            lblCurrentHDD.Visible = true;
            lblCurrentHDD0.Visible = true;
        }

        protected void ddlHDD6_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgHDD1, ddlHDD6, lblHDD1Spec, lblHDD1Price, "HDD6");
            lblCurrentHDD0.Text = "6";
            lblCurrentHDD.Visible = true;
            lblCurrentHDD0.Visible = true;
        }

        protected void ddlSoundcard_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgSoundcard, ddlSoundcard, lblSoundSpec, lblSoundPrice, "Soundcard");
        }

        protected void ddlPSU_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgPSU, ddlPSU, lblPSUSpec, lblPSUPrice, "PSU");
        }

        protected void ddlCasing_SelectedIndexChanged(object sender, EventArgs e)
        {
            getDetails(imgCasing, ddlCasing, lblCasingSpec, lblCasingPrice, "Casing");
        }
        #endregion

        protected void getDetails(Image img, DropDownList ddl, Label lblspec, Label price, string catString)
        {
            if (ddl.SelectedValue != "0")
            {
                Products p = EntityLogic.RetrieveProductByID(Convert.ToInt32(ddl.SelectedValue));
                img.ImageUrl = p.ProdImg;
                img.Height = 100;
                img.Width = 100;

                Dictionary<string, string> pFlag = BusinessLogic.DelimitCSV(p.Flag);
                Dictionary<string, string> pSpec = BusinessLogic.DelimitCSV(p.Specification);
                string s = "";
                StringBuilder sb = new StringBuilder();
                
                switch (p.Category)
                {
                    case "Motherboard":
                        lblspec.Text = "";
                        lblspec.Text = "Socket: " + pFlag["socket"] + " (" + pSpec["cpu"] + ")<br>" +
                            "RAM Slots: " + pFlag["ramN"] + " x " + pFlag["ram"]
                            + "<br>RAM Speeds Supported: ";
                        
                        
                        if (Convert.ToInt32(pSpec["rss2000"]) == 1)
                        {
                            s += "2000/";
                        }

                        if (Convert.ToInt32(pSpec["rss1866"]) == 1)
                        {
                            s += "1866/";
                        }

                        if (Convert.ToInt32(pSpec["rss1800"]) == 1)
                        {
                            s += "1800/";
                        }

                        if (Convert.ToInt32(pSpec["rss1600"]) == 1)
                        {
                            s += "1600/";
                        }

                        if (Convert.ToInt32(pSpec["rss1333"]) == 1)
                        {
                            s += "1333/";
                        }

                        if (Convert.ToInt32(pSpec["rss1066"]) == 1)
                        {
                            s += "1066/";
                        }
                        lblspec.Text += s.TrimEnd('/') + "MHz<br>Expansion Slots :";
                        s = "";

                        if(pFlag.ContainsKey("exp1"))
                        {
                            s += pFlag["exp1N"] + " x " + pFlag["exp1"] + " / ";
                        }
                        if (pFlag.ContainsKey("exp2"))
                        {
                            s += pFlag["exp2N"] + " x " + pFlag["exp2"] + " / ";
                        }
                        if (pFlag.ContainsKey("exp3"))
                        {
                            s += pFlag["exp3N"] + " x " + pFlag["exp3"] + " / ";
                        }
                        if (pFlag.ContainsKey("exp4"))
                        {
                            s += pFlag["exp4N"] + " x " + pFlag["exp4"] + " / ";
                        }
                        if (pFlag.ContainsKey("exp5"))
                        {
                            s += pFlag["exp5N"] + " x " + pFlag["exp5"] + " / ";
                        }
                        lblspec.Text += s.TrimEnd('/', ' ');
                        s = "";

                        break;

                    case "CPU":

                        sb.Append("Frequency: ");
                        if (Convert.ToInt32(pSpec["freq"]) > 999)
                        {
                            double d = Convert.ToInt32(pSpec["freq"]) / 1000;
                            sb.Append(d.ToString() + " GHz<br>");
                        }
                        else
                        {
                            sb.Append(Convert.ToInt32(pSpec["freq"]) + "MHz<br>");
                        }
                        sb.Append("Socket: " + pFlag["socket"]);

                        lblspec.Text = sb.ToString();
                        sb.Clear();
                        break;

                    case "RAM":
                        sb.Append("Class: " + pFlag["class"]);
                        sb.Append(" " + pFlag["slot"] + " x " + pFlag["mem"] + "GB");

                        if (pFlag.ContainsKey("freq"))
                        {
                            sb.Append("<br>Frequency: " + pFlag["freq"] + "MHz");
                            lblspec.Text = sb.ToString();
                        }

                        if (pSpec.ContainsKey("dram"))
                        {
                            sb.Append("<br>DRAM: " + pSpec["dram"] + "MB x " + pSpec["dramN"]);
                        }

                        if(pSpec.ContainsKey("timing"))
                        {
                            sb.Append("<br>Clock speed: " + pSpec["timing"]);
                        }
                        lblMemSpec.Text = sb.ToString();
                        sb.Clear();
                        break;

                    case "GFX":
                        sb.Append("Graphic RAM: GDDR" + pSpec["gddrclass"] + " x " + pSpec["mem"])
                            .Append("<br>Supports DirectX: DX" + pSpec["DX"])
                            .Append("<br>Default CPU/Memory Clock: " + pSpec["cpuclock"] + " / " + pSpec["memclock"]);

                        if (pFlag.ContainsKey("SLI"))
                        {
                            if (pFlag["SLI"] == "1")
                            {
                                sb.Append("<br>SLI: Yes");
                            }
                        }
                        if (pFlag.ContainsKey("CrossFireX"))
                        {
                            if (pFlag["CrossFireX"] == "1")
                            {
                                sb.Append("<br>CrossFireX: Yes");
                            }
                        }

                        lblGFXSpec.Text = sb.ToString();
                        sb.Clear();
                        break;

                    case "HDD":

                        sb.Append("Type: " + pFlag["type"] + "<br>Storage: ");
                        if (Convert.ToInt32(pSpec["storage"]) > 999)
                        {
                            double d = Convert.ToInt32(pSpec["storage"]) / 1000;
                            sb.Append(d.ToString() + " TB");
                        }
                        else
                        {
                            sb.Append(Convert.ToInt32(pSpec["storage"]) + "GB");
                        }
                        sb.Append("/" + pSpec["buffer"]);
                        lblHDD1Spec.Text = sb.ToString();
                        sb.Clear();
                        break;

                    case "Soundcard":
                        sb.Append("Supports Surround: " + pSpec["surround"]);
                        lblSoundSpec.Text = sb.ToString();
                        sb.Clear();

                        break;

                    case "PSU":
                        sb.Append("Capacity: " + pFlag["capacity"] + "W");
                        lblPSUSpec.Text = sb.ToString();
                        sb.Clear();
                        break;

                    case "Casing":
                        sb.Append("Form factor: " + pFlag["form"]);
                        lblCasingSpec.Text = sb.ToString();
                        sb.Clear();
                        break;
                }
                
                price.Text = p.Price.ToString();
                lblspec.Visible = true;
                price.Visible = true;
                UpdateCustomPrice(catString, Convert.ToDouble(p.Price));
            }
            else
            {
                img.ImageUrl = "images/loading.gif";
                img.Height = 16;
                img.Width = 16;
                lblspec.Visible = false;
                price.Visible = false;
                UpdateCustomPrice(catString, 0);
            }
            img.Visible = true;
        }



        protected void UpdateCustomPrice(string category, double newPrice)
        {
            Dictionary<string, double> d = (Dictionary<string, double>) Session["customPrice"];

            if (d[category] == 0)
            {
                
                d[category] = newPrice;
                d["Total"] += newPrice;
            }
            else
            {
                d["Total"] -= d[category];
                d[category] = newPrice;
                d["Total"] += newPrice;
            }
            if (category == "HDD1" || category == "HDD2" || category == "HDD3" || category == "HDD4" || category == "HDD5" || category == "HDD6")
            {
                double hddtotal = 0;
                string cat = "HDD";

                for (int i = 1; i < 7; i++)
                {
                    cat = "HDD" + i.ToString();
                    hddtotal += d[cat];
                    cat = "HDD";
                }
                lblHDDTPrice.Text = hddtotal.ToString();
                lblHDDTPrice.Visible = true;
            }

            lblTotal0.Text = d["Total"].ToString();
            lblTotal0.Visible = true;
            Label10.Visible = true;
        }

        protected void ResetCustomPrice(Dictionary<string, double> customPrice)
        {
            customPrice.Clear();
            customPrice.Add("MB", 0);
            customPrice.Add("CPU", 0);
            customPrice.Add("RAM", 0);
            customPrice.Add("GFX", 0);
            customPrice.Add("HDD1", 0);
            customPrice.Add("HDD2", 0);
            customPrice.Add("HDD3", 0);
            customPrice.Add("HDD4", 0);
            customPrice.Add("HDD5", 0);
            customPrice.Add("HDD6", 0);
            customPrice.Add("Soundcard", 0);
            customPrice.Add("PSU", 0);
            customPrice.Add("Casing", 0);
            customPrice.Add("Total", 0);
        }

        protected void requiredChk()
        {
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
            Dictionary<string, int> d = new Dictionary<string, int>();
            d.Add("MB", Convert.ToInt32(ddlMB.SelectedValue));
            d.Add("CPU", Convert.ToInt32(ddlCPU.SelectedValue));
            d.Add("RAM", Convert.ToInt32(ddlMemory.SelectedValue));
            if(ddlGFX.SelectedValue!="")
            {
            d.Add("GFX1", Convert.ToInt32(ddlGFX.SelectedValue));
            if (chkbxDualGFX.Checked == true)
            {
                d.Add("GFX2", Convert.ToInt32(ddlGFX.SelectedValue));
            }
            }
            d.Add("HDD1", Convert.ToInt32(ddlHDD1.SelectedValue));
            d.Add("HDD2", Convert.ToInt32(ddlHDD2.SelectedValue));
            d.Add("HDD3", Convert.ToInt32(ddlHDD3.SelectedValue));
            d.Add("HDD4", Convert.ToInt32(ddlHDD4.SelectedValue));
            d.Add("HDD5", Convert.ToInt32(ddlHDD5.SelectedValue));
            d.Add("HDD6", Convert.ToInt32(ddlHDD6.SelectedValue));
            if(ddlSoundcard.SelectedValue!="")
            {
            d.Add("Soundcard", Convert.ToInt32(ddlSoundcard.SelectedValue));
            }
            d.Add("PSU", Convert.ToInt32(ddlPSU.SelectedValue));
            d.Add("Casing", Convert.ToInt32(ddlCasing.SelectedValue));

            int requiredChk = d["MB"] * d["CPU"] * d["RAM"] * (d["HDD1"] + d["HDD2"] + d["HDD3"] + d["HDD4"] + d["HDD5"] + d["HDD6"]) * d["PSU"] * d["Casing"];
            

            if (requiredChk != 0)
            {
                int createdSetID = EntityLogic.CreateSet(d, Convert.ToInt32(Session["AccountID"]));
                if (createdSetID != -1)
                {
                    Session.Remove("customPrice");
                    Session["createdSetID"] = createdSetID;
                    Response.Redirect("RetrieveSets.aspx");
                }
                else
                {
                    lblSetCreateError.Text = "Failed to create set!";
                }
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                int hddChk = 0;
                sb.Append("Failed to create set! Please select at least one part for each of the following :");
                foreach (KeyValuePair<string, int> kvp in d)
                {
                    if (kvp.Value == 0)
                    {
                        switch (kvp.Key)
                        {
                            case "MB":
                                sb.Append("<br>Motherboard");
                                lblMB.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "CPU":
                                sb.Append("<br>CPU");
                                lblCPU.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "RAM":
                                sb.Append("<br>Memory");
                                lblMemory.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "HDD1":
                                hddChk++;
                                break;
                            case "HDD2":
                                hddChk++;
                                break;
                            case "HDD3":
                                hddChk++;
                                break;
                            case "HDD4":
                                hddChk++;
                                break;
                            case "HDD5":
                                hddChk++;
                                break;
                            case "HDD6":
                                hddChk++;
                                break;
                            case "PSU":
                                sb.Append("<br>Power Supply");
                                lblPSU.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "Casing":
                                sb.Append("<br>Casing");
                                lblCasing.ForeColor = System.Drawing.Color.Red;
                                break;
                        }
                    }
                }
                if (hddChk > 0)
                {
                    sb.Append("<br>Harddisk");
                    lblHDD.ForeColor = System.Drawing.Color.Red;
                }
                lblSetCreateError.Text = sb.ToString();
            }
            
        }

        #region Redirect to ProductInfo
        protected void imgMB_Click(object sender, ImageClickEventArgs e)
        {
            //imgMB.Attributes.Add("onclick", "JavaScript: window.opener.location.href ='ProductInfo.aspx?ProductID=" + ddlMB.SelectedValue + "';");
            //Session["ProductID"] = Convert.ToInt32(ddlMB.SelectedValue);
            Response.Redirect("ProductInfo.aspx?ProductID=" + ddlMB.SelectedValue);
        }

        protected void imgCPU_Click(object sender, ImageClickEventArgs e)
        {
            //Session["ProductID"] = Convert.ToInt32(ddlCPU.SelectedValue);
            Response.Redirect("ProductInfo.aspx?ProductID=" + ddlCPU.SelectedValue);
        }

        protected void imgMemory_Click(object sender, ImageClickEventArgs e)
        {
            //Session["ProductID"] = Convert.ToInt32(ddlMemory.SelectedValue);
            Response.Redirect("ProductInfo.aspx?ProductID=" + ddlMemory.SelectedValue);
        }

        protected void imgGFX_Click(object sender, ImageClickEventArgs e)
        {
            
            //Session["ProductID"] = Convert.ToInt32(ddlGFX.SelectedValue);
            Response.Redirect("ProductInfo.aspx?ProductID=" + ddlGFX.SelectedValue);

        }

        protected void imgHDD1_Click(object sender, ImageClickEventArgs e)
        {
            string selectedVal = "";
            switch (lblCurrentHDD0.Text)
            {
                case "0":
                    selectedVal = "1";
                    break;
                case "1":
                    selectedVal = ddlHDD1.SelectedValue;
                    break;

                case "2":
                    selectedVal = ddlHDD2.SelectedValue;
                    break;

                case "3":
                    selectedVal = ddlHDD3.SelectedValue;
                    break;

                case "4":
                    selectedVal = ddlHDD4.SelectedValue;
                    break;

                case "5":
                    selectedVal = ddlHDD5.SelectedValue;
                    break;

                case "6":
                    selectedVal = ddlHDD6.SelectedValue;
                    break;
            }

            Response.Redirect("ProductInfo.aspx?ProductID=" + selectedVal); 
        }

        protected void imgSoundcard_Click(object sender, ImageClickEventArgs e)
        {
            Session["ProductID"] = Convert.ToInt32(ddlCPU.SelectedValue);
            Response.Redirect("ProductInfo.aspx");
        }

        protected void imgPSU_Click(object sender, ImageClickEventArgs e)
        {
            Session["ProductID"] = Convert.ToInt32(ddlCPU.SelectedValue);
            Response.Redirect("ProductInfo.aspx");
        }

        protected void imgCasing_Click(object sender, ImageClickEventArgs e)
        {
            Session["ProductID"] = Convert.ToInt32(ddlCPU.SelectedValue);
            Response.Redirect("ProductInfo.aspx");
        }
        #endregion

        protected void populateCustomizeEditFromSet(int setID)
        {
            List<Products> pList = EntityLogic.RetrieveSetContents(setID);
            Dictionary<string, string> mbFlag = new Dictionary<string, string>();
            Dictionary<int, int> hddQuantity = new Dictionary<int, int>();

            int val = 0;
            
            foreach (Products p in pList)
            {
                if (p.Category == "Motherboard")
                {
                    mbFlag = BusinessLogic.DelimitCSV(p.Flag);
                    val = Convert.ToInt32(p.Type);
                }
            }
            int hddCount = 0;
            foreach (Products p in pList)
            {
                ListItem li;
                
                switch (p.Category)
                {
                    case "Motherboard":

                        List<Products> proList = EntityLogic.RetrieveProducts("Motherboard", Convert.ToInt32(p.Type));

                        li = new ListItem("[Select One]", "0");
                        ddlMB.Items.Add(li);
                        foreach (Products pro in proList)
                        {
                            li = new ListItem(pro.ProductName, pro.ProductID.ToString());
                            ddlMB.Items.Add(li);
                        }

                        if (Convert.ToInt32(mbFlag["SLI"]) == 1 || Convert.ToInt32(mbFlag["CrossFireX"]) == 1)
                        {
                            chkbxDualGFX.Visible = true;
                        }

                        ddlMB.SelectedValue = p.ProductID.ToString();
                        getDetails(imgMB, ddlMB, lblMBSpec, lblMBPrice, "MB");
                        lblMsg2.Visible = true;
                        lblMB.Visible = true;
                        ddlMB.Visible = true;
                        Label2.Visible = true;
                        ddlMB.Enabled = false;

                        break;

                    case "CPU":
                        List<Products> cpuList = EntityLogic.RetrieveProducts("CPU", val, mbFlag["socket"]);
                        populateDDL(cpuList, ddlCPU);
                        ddlCPU.SelectedValue = p.ProductID.ToString();
                        getDetails(imgCPU, ddlCPU, lblCPUSpec, lblCPUPrice, "CPU");

                        break;

                    case "RAM":
                        List<Products> memList = EntityLogic.RetrieveProducts("RAM", val, mbFlag["ram"]);
                        populateDDL(memList, ddlMemory);
                        ddlMemory.SelectedValue = p.ProductID.ToString();
                        getDetails(imgMemory, ddlMemory, lblMemSpec, lblMemPrice, "RAM");
                        break;

                    case "GFX":
                        List<Products> gfxList = EntityLogic.RetrieveProducts("GFX", val, mbFlag["exp1"]);
                        populateDDL(gfxList, ddlGFX);

                        if (EntityLogic.RetrieveSetItemQuantity(p, Convert.ToInt32(Session["selectEditSet"])) == 2)
                        {
                            chkbxDualGFX.Checked = true;
                        }
                        else
                        {
                            chkbxDualGFX.Checked = false;
                        }
                        ddlGFX.SelectedValue = p.ProductID.ToString();
                        getDetails(imgGFX, ddlGFX, lblGFXSpec, lblGFXPrice, "GFX");
                        break;

                    case "HDD":

                        if (hddCount == 0)
                        {
                            List<Products> hddList = EntityLogic.RetrieveProducts("HDD", val, mbFlag["hdd"]);
                            populateHDD(Convert.ToInt32(mbFlag["hddN"]), hddList);
                            hddCount++;
                        }

                        hddQuantity.Add(p.ProductID, EntityLogic.RetrieveSetItemQuantity(p, Convert.ToInt32(Session["selectEditSet"])));
                        

                            

                        break;

                    case "Soundcard":
                        List<Products> scList = EntityLogic.RetrieveProducts("Soundcard", val, mbFlag["exp2"]);
                        populateDDL(scList, ddlSoundcard);
                        ddlSoundcard.SelectedValue = p.ProductID.ToString();
                        getDetails(imgSoundcard, ddlSoundcard, lblSoundSpec, lblSoundPrice, "Soundcard");
                        break;

                    case "PSU":
                        List<Products> psuList = EntityLogic.RetrieveProducts("PSU", val);
                        populateDDL(psuList, ddlPSU);
                        ddlPSU.SelectedValue = p.ProductID.ToString();
                        getDetails(imgPSU, ddlPSU, lblPSUSpec, lblPSUPrice, "PSU");
                        break;

                    case "Casing":
                        List<Products> casingList = EntityLogic.RetrieveProducts("Casing", val, mbFlag["form"]);
                        populateDDL(casingList, ddlCasing);
                        ddlCasing.SelectedValue = p.ProductID.ToString();
                        getDetails(imgCasing, ddlCasing, lblCasingSpec, lblCasingPrice, "Casing");
                        break;
                }
            }

            int i = 0, quan = 0, count = 0; ;
            string h = "HDD";
            List<DropDownList> ddlList = new List<DropDownList>();
            ddlList.Add(ddlHDD1);
            ddlList.Add(ddlHDD2);
            ddlList.Add(ddlHDD3);
            ddlList.Add(ddlHDD4);
            ddlList.Add(ddlHDD5);
            ddlList.Add(ddlHDD6);

            //quan = hddQuantity.First().Value;
            foreach (KeyValuePair<int, int> kvp in hddQuantity)
            {
                quan += kvp.Value;
                for (i = count; i < quan; i++)
                {
                    ddlList[i].SelectedValue = kvp.Key.ToString();
                    int temp = i + 1;
                    h = "HDD" + temp.ToString();
                    getDetails(imgHDD1, ddlList[i], lblHDD1Spec, lblHDD1Price, h);
                    
                    
                }
                count = quan;
                
            }

            List<int> newList = new List<int>();
            newList.Add(Convert.ToInt32(ddlMB.SelectedValue));
            newList.Add(Convert.ToInt32(ddlCPU.SelectedValue));
            newList.Add(Convert.ToInt32(ddlMemory.SelectedValue));
            if (ddlGFX.SelectedValue != "")
            {
                newList.Add(Convert.ToInt32(ddlGFX.SelectedValue));
                if (chkbxDualGFX.Checked == true)
                {
                    newList.Add(Convert.ToInt32(ddlGFX.SelectedValue));
                }
            }
            newList.Add(Convert.ToInt32(ddlHDD1.SelectedValue));
            newList.Add(Convert.ToInt32(ddlHDD2.SelectedValue));
            newList.Add(Convert.ToInt32(ddlHDD3.SelectedValue));
            newList.Add(Convert.ToInt32(ddlHDD4.SelectedValue));
            newList.Add(Convert.ToInt32(ddlHDD5.SelectedValue));
            newList.Add(Convert.ToInt32(ddlHDD6.SelectedValue));
            if (ddlSoundcard.SelectedValue != "")
            {
                newList.Add(Convert.ToInt32(ddlSoundcard.SelectedValue));
            }
            newList.Add(Convert.ToInt32(ddlPSU.SelectedValue));
            newList.Add(Convert.ToInt32(ddlCasing.SelectedValue));
            List<int> oldList = new List<int>();
            foreach (int pID in newList)
            {
                if (pID != 0)
                {
                    oldList.Add(pID);
                }
            }
            Session["oldList"] = oldList;

            lblMsg3.Visible = true;
            lblCPU.Visible = true;
            ddlCPU.Visible = true;
            Label3.Visible = true;
            lblMemory.Visible = true;
            ddlMemory.Visible = true;
            Label4.Visible = true;
            lblGFX.Visible = true;
            ddlGFX.Visible = true;
            Label5.Visible = true;
            btnSubmit.Visible = true;
            lblSound.Visible = true;
            ddlSoundcard.Visible = true;
            Label7.Visible = true;
            lblPSU.Visible = true;
            ddlPSU.Visible = true;
            Label8.Visible = true;
            lblCasing.Visible = true;
            ddlCasing.Visible = true;
            Label9.Visible = true;

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> d = new Dictionary<string, int>();
            d.Add("MB", Convert.ToInt32(ddlMB.SelectedValue));
            d.Add("CPU", Convert.ToInt32(ddlCPU.SelectedValue));
            d.Add("RAM", Convert.ToInt32(ddlMemory.SelectedValue));
            if (ddlGFX.SelectedValue != "")
            {
                d.Add("GFX1", Convert.ToInt32(ddlGFX.SelectedValue));
                if (chkbxDualGFX.Checked == true)
                {
                    d.Add("GFX2", Convert.ToInt32(ddlGFX.SelectedValue));
                }
            }
            d.Add("HDD1", Convert.ToInt32(ddlHDD1.SelectedValue));
            d.Add("HDD2", Convert.ToInt32(ddlHDD2.SelectedValue));
            d.Add("HDD3", Convert.ToInt32(ddlHDD3.SelectedValue));
            d.Add("HDD4", Convert.ToInt32(ddlHDD4.SelectedValue));
            d.Add("HDD5", Convert.ToInt32(ddlHDD5.SelectedValue));
            d.Add("HDD6", Convert.ToInt32(ddlHDD6.SelectedValue));
            if (ddlSoundcard.SelectedValue != "")
            {
                d.Add("Soundcard", Convert.ToInt32(ddlSoundcard.SelectedValue));
            }
            d.Add("PSU", Convert.ToInt32(ddlPSU.SelectedValue));
            d.Add("Casing", Convert.ToInt32(ddlCasing.SelectedValue));

            int requiredChk = d["MB"] * d["CPU"] * d["RAM"] * (d["HDD1"] + d["HDD2"] + d["HDD3"] + d["HDD4"] + d["HDD5"] + d["HDD6"]) * d["PSU"] * d["Casing"];

            if (requiredChk != 0)
            {
                List<int> newList = new List<int>();
                newList.Add(Convert.ToInt32(ddlMB.SelectedValue));
                newList.Add(Convert.ToInt32(ddlCPU.SelectedValue));
                newList.Add(Convert.ToInt32(ddlMemory.SelectedValue));
                if (ddlGFX.SelectedValue != "")
                {
                    newList.Add(Convert.ToInt32(ddlGFX.SelectedValue));
                    if (chkbxDualGFX.Checked == true)
                    {
                        newList.Add(Convert.ToInt32(ddlGFX.SelectedValue));
                    }
                }
                newList.Add(Convert.ToInt32(ddlHDD1.SelectedValue));
                newList.Add(Convert.ToInt32(ddlHDD2.SelectedValue));
                newList.Add(Convert.ToInt32(ddlHDD3.SelectedValue));
                newList.Add(Convert.ToInt32(ddlHDD4.SelectedValue));
                newList.Add(Convert.ToInt32(ddlHDD5.SelectedValue));
                newList.Add(Convert.ToInt32(ddlHDD6.SelectedValue));
                if (ddlSoundcard.SelectedValue != "")
                {
                    newList.Add(Convert.ToInt32(ddlSoundcard.SelectedValue));
                }
                newList.Add(Convert.ToInt32(ddlPSU.SelectedValue));
                newList.Add(Convert.ToInt32(ddlCasing.SelectedValue));

                EntityLogic.RetrieveSetContents(Convert.ToInt32(Session["selectEditSet"]));

                List<int> oldList = (List<int>)Session["oldList"];

                BusinessLogic.UpdateSet(oldList, newList, Convert.ToInt32(Session["selectEditSet"]));

                Session["createdSetID"] = Session["selectEditSet"];
                Response.Redirect("RetrieveSets.aspx");
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                int hddChk = 0;
                sb.Append("Failed to update set! Please select at least one part for each of the following :");
                foreach (KeyValuePair<string, int> kvp in d)
                {
                    if (kvp.Value == 0)
                    {
                        switch (kvp.Key)
                        {
                            case "MB":
                                sb.Append("<br>Motherboard");
                                lblMB.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "CPU":
                                sb.Append("<br>CPU");
                                lblCPU.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "RAM":
                                sb.Append("<br>Memory");
                                lblMemory.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "HDD1":
                                hddChk++;
                                break;
                            case "HDD2":
                                hddChk++;
                                break;
                            case "HDD3":
                                hddChk++;
                                break;
                            case "HDD4":
                                hddChk++;
                                break;
                            case "HDD5":
                                hddChk++;
                                break;
                            case "HDD6":
                                hddChk++;
                                break;
                            case "PSU":
                                sb.Append("<br>Power Supply");
                                lblPSU.ForeColor = System.Drawing.Color.Red;
                                break;
                            case "Casing":
                                sb.Append("<br>Casing");
                                lblCasing.ForeColor = System.Drawing.Color.Red;
                                break;
                        }
                    }
                }
                if (hddChk > 0)
                {
                    sb.Append("<br>Harddisk");
                    lblHDD.ForeColor = System.Drawing.Color.Red;
                }
                lblSetCreateError.Text = sb.ToString();
            }


        }
    }
}